(function(){
	angular.module("webApp").controller("OrderCtrl",OrderCtrl);
	OrderCtrl.$inject=["$rootScope","$scope","dataService","helper","$location","$window","$timeout","cacheService"];
	function OrderCtrl($rootScope,$scope,dataService,helper,$location,$window,$timeout,cacheService){
		var tab = new auiTab({
	        element:document.getElementById("orderTabs"),
	        index:1,
	        repeatClick:true
	    },function(ret){
	        console.log(ret);
	    });
		$scope.listAction=function(){
				
			$scope.orderGet();
		}
		$scope.ordercancel=function(order_id,event){
			dataService.orderStatusUpdate(order_id,event).then(function(result){
				if(result.Msg){
					helper.myAlert("error:"+result.Msg);
				}else{
					$scope.orderGet();
				}
			})
		}
		$scope.orderGet=function(){
			var query={page_no:1,page_size:20};
			if($scope.status){
				query.query={status:$scope.status};
			}
			// query.query["member_id"]=cacheService.get("currentUserInfo").user_id;
			dataService.orderGet(query).then(function(result){
				if(result.Msg){
					helper.myAlert("error:"+result.Msg);
				}else{
					$scope.orders=result.orders;
				}
			})
		}
		$scope.contactMerchant=function(){
			$rootScope.$broadcast("event:contactBusiness");
			// $("#contactd").popup();
			// $scope.img="/static/uploads/2016-11-24/d95b5a76-c7b0-4151-b671-7ea57524a0e3.jpg";
		}
		$scope.deliverGet=function(order_id){
			var query={};
			query.query={order_id:order_id}
			dataService.deliverGet(query).then(function(){

			})
		}
		$scope.reviewAction=function(){
			$scope.review={};
			var order_id=$location.search().order_id;
			var item_id=$location.search().item_id;
			console.log("222",order_id,item_id);
			$scope.review.score={};
			$scope.review.content={};
			dataService.orderOne(order_id).then(function(order){
				$scope.order=order;
				$scope.items=[];
				_.map($scope.order.items,function(item){
					if(item.item_id==item_id){
						item.sku_checked=true;
						$scope.items.push(item);
					}else if(item_id==undefined){
						console.log("111");
						item.sku_checked=true;
						$scope.review.score[item.item_id]=undefined;
						$scope.items.push(item);
					}
				});

			})
		}
		$scope.reviewAdd=function(){
			$scope.reviewError=undefined;
			$scope.review.partner_id=$scope.order.partner_id;
			$scope.review.order_id=$scope.order.order_id;
			console.log($scope.review.score);
			if(_.size($scope.review.score)==0){
				helper.myAlert("请评价商品");
				return false;
			}
			console.log($scope.items);
			_.map($scope.items,function(item){
				var review={};
			    review.image=item.image;
				review.item_id=item.item_id;
				review.partner_id=$scope.order.partner_id;
				review.order_id=$scope.order.order_id;
				if($scope.review.score[item.item_id]==undefined){
					helper.myAlert("没有评价",item.title);
					return false;
				}
				review.score=$scope.review.score[item.item_id];
				review.content=$scope.review.content[item.item_id];
				console.log("11",review);
				if(!item.sku_checked){
					return false;
				}
				console.log("执行几次");
				dataService.reviewAdd(review).then(function(result){
					if(result.Msg){
						$scope.reviewError=result;
					}else{
						if($scope.reviewError==undefined){
							$scope.reviewError=result;
						}
					} 
				});
			});
			$timeout(function(){
				if($scope.reviewError.Msg){
					helper.myAlert("error:"+$scope.reviewError.Msg);
				}else{
					helper.myAlert("success:","#/order/list");
				}
			},500)
			console.log($scope.review.score);
			
		}
		$scope.payConfirm=function(order,url){
			$location.url("buy/payconfirm?order_id="+order.order_id+"&url="+url);
		}
		$scope.querypinglun=function(item_id){
			var query={};
			query.query={item_id:item_id}
			dataService.reviewGet(query).then(function(result){

			})
		}
		$scope.hrefdetails=function($event,order_id){
			if($event.target.name!="goods_review"){
				$location.url("order/details?order_id="+order_id)
			}
		}
		$scope.hrefItemInfo=function(item){
			$location.url("item/info?item_id="+item.item_id);
		}
		$scope.orderDetails=function(){
			var order_id= $location.search().order_id;
			dataService.orderOne(order_id).then(function(result){
				$scope.order=result;
			})
		}
	}	
})()